package problem1839;

import java.util.HashMap;

//1839.所有元音按顺序排布的最长子字符串
//https://leetcode.cn/problems/longest-substring-of-all-vowels-in-order/description/
//暴力解法

class Solution {
    public int longestBeautifulSubstring(String word) {
        int n = word.length();
        HashMap<Character, Integer> map = new HashMap<>();
        map.put('a', 1);
        map.put('e', 2);
        map.put('i', 3);
        map.put('o', 4);
        map.put('u', 5);
        int[] hash = new int[6];
        hash[0] = 1;
        int ret = 0, count = 0;
        for(int i = 0, j =0; j<n; j++) {
            int index = map.get(word.charAt(j));
            if(hash[index] == 0) count++;
            hash[index]++;

            while(i < j && !check(hash, index)) {
                int index2 = map.get(word.charAt(i));
                hash[index2]--;
                if(hash[index2] == 0) count--;
                i++;
            }

            if(count == 5) {
                if(j-i+1 > ret) {
                    ret = j-i+1;
                }
            }
        }
        return ret;
    }
    public boolean check(int[] hash, int k) {
        //k之前全部大于0 
        for(int i = 1; i<k; i++) {
            if(hash[i] == 0) return false;
        }

        //k之后全部小于0
        for(int i = k+1; i<hash.length; i++) {
            if(hash[i] > 0) return false;
        }
        return true;
    }
}